package com.wfh.rpc.fault.retry;

import com.github.rholder.retry.*;
import com.wfh.rpc.model.RpcResponse;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

/**
 * @Title: FixedIntervalRetryStrategy
 * @Author wangfenghuan
 * @Package com.wfh.rpc.fault.retry
 * @Date 2025/10/17 08:28
 * @description: 固定事件重试
 */
@Slf4j
public class FixedIntervalRetryStrategy implements RetryStrategy{

    /**
     * 重试
     * @param callable
     * @return
     * @throws Exception
     */
    @Override
    public RpcResponse dRetry(Callable<RpcResponse> callable) throws Exception {
        Retryer<RpcResponse> build = RetryerBuilder.<RpcResponse>newBuilder()
                .retryIfExceptionOfType(Exception.class)
                .withWaitStrategy(WaitStrategies.fixedWait(3L, TimeUnit.SECONDS))
                .withRetryListener(new RetryListener() {
                    @Override
                    public <V> void onRetry(Attempt<V> attempt) {
                        log.info("重试次数： {}", attempt.getAttemptNumber());
                    }
                }).build();
        return build.call(callable);
    }
}
